package cn.iocoder.yudao.module.ao.service.building;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.ao.controller.admin.building.vo.*;
import cn.iocoder.yudao.module.ao.dal.dataobject.building.BuildingDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.ao.dal.mysql.building.BuildingMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.ao.enums.ErrorCodeConstants.*;

/**
 * 建筑物信息 Service 实现类
 *
 * @author 禾安
 */
@Service
@Validated
public class BuildingServiceImpl implements BuildingService {

    @Resource
    private BuildingMapper buildingMapper;

    @Override
    public Long createBuilding(BuildingSaveReqVO createReqVO) {
        // 插入
        BuildingDO building = BeanUtils.toBean(createReqVO, BuildingDO.class);
        buildingMapper.insert(building);

        // 返回
        return building.getBuildingId();
    }

    @Override
    public void updateBuilding(BuildingSaveReqVO updateReqVO) {
        // 校验存在
        validateBuildingExists(updateReqVO.getBuildingId());
        // 更新
        BuildingDO updateObj = BeanUtils.toBean(updateReqVO, BuildingDO.class);
        buildingMapper.updateById(updateObj);
    }

    @Override
    public void deleteBuilding(Long id) {
        // 校验存在
        validateBuildingExists(id);
        // 删除
        buildingMapper.deleteById(id);
    }

    @Override
        public void deleteBuildingListByIds(List<Long> ids) {
        // 删除
        buildingMapper.deleteByIds(ids);
        }


    private void validateBuildingExists(Long id) {
        if (buildingMapper.selectById(id) == null) {
            throw exception(BUILDING_NOT_EXISTS);
        }
    }

    @Override
    public BuildingDO getBuilding(Long id) {
        return buildingMapper.selectById(id);
    }

    @Override
    public PageResult<BuildingDO> getBuildingPage(BuildingPageReqVO pageReqVO) {
        return buildingMapper.selectPage(pageReqVO);
    }

}